package org.neptune.service.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author lwl
 * @date 2019/7/25 19:42
 */
public class IpUtil {

	private static final Logger logger = LoggerFactory.getLogger(IpUtil.class);

	public static String getIpAddr(HttpServletRequest request) {
		String ip = null;
		try{
			ip = request.getHeader("x-forwarded-for");
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)){
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)){
				ip = request.getHeader("HTTP_CLIENT_IP");
			}
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)){
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			}
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)){
				ip = request.getRemoteAddr();
			}
		} catch (Exception e){
			logger.error("IPUtils ERROR ", e);
		}
		// 使用代理，则获取第一个IP地址
		if (StringUtils.isNotEmpty(ip) && ip.length() > 15){
			if (ip.indexOf(",") > 0){
				ip = ip.substring(0, ip.indexOf(","));
			}
		}
		return ip;
	}
}
